﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.SharePoint;

namespace UnitTest
{
    [TestClass]
    public class WriteEvent
    {
        [TestInitialize]
        public void Init()
        {
            this.site = new SPSite("http://xchai1");
            this.web = site.OpenWeb();
            this.calendar = web.GetList("/Lists/Calendar");

            if (this.web.CurrentUser != null
                && this.web.CurrentUser.RegionalSettings != null)
                this.regionalSettings = this.web.CurrentUser.RegionalSettings;
            else
                this.regionalSettings = this.web.RegionalSettings;

        }

        [TestCleanup]
        public void Cleanup()
        {
            this.web.Dispose();
            this.site.Dispose();
        }

        SPSite site;
        SPWeb web;
        SPList calendar;
        SPRegionalSettings regionalSettings;

        [TestMethod]
        public void WriteSimpleEvent()
        {
            var item = this.calendar.GetItemById(4);
            var evt = WindStyle.SPCalendar.Event.ParseEvent(item, this.regionalSettings);            
            var realEvent = evt as WindStyle.SPCalendar.Event<WindStyle.SPCalendar.Pattern.SimplePattern>;

            var newItem = this.calendar.AddItem();
            WindStyle.SPCalendar.Event.ApplyEvent(realEvent, newItem, this.regionalSettings);

            Assert.AreEqual(item.Title, 
                newItem.Title);

            Assert.AreEqual(item[SPBuiltInFieldId.Location],
                newItem[SPBuiltInFieldId.Location]);

            Assert.AreEqual(item[SPBuiltInFieldId.StartDate],
                newItem[SPBuiltInFieldId.StartDate]);

            Assert.AreEqual(item[SPBuiltInFieldId.fAllDayEvent],
                newItem[SPBuiltInFieldId.fAllDayEvent]);

            Assert.IsFalse(evt.IsRecurrence);

            Assert.AreEqual(item[SPBuiltInFieldId.Category],
                newItem[SPBuiltInFieldId.Category]);

            Assert.AreEqual(item[SPBuiltInFieldId.Workspace],
                newItem[SPBuiltInFieldId.Workspace]);
        }

        [TestMethod]
        public void WriteDailyByFrequencyEvent()
        {
            var item = this.calendar.GetItemById(5);
            var evt = WindStyle.SPCalendar.Event.ParseEvent(item, this.regionalSettings);
            var realEvent = evt as WindStyle.SPCalendar.Event<WindStyle.SPCalendar.Pattern.DailyByFrequencyPattern>;

            var newItem = this.calendar.AddItem();
            WindStyle.SPCalendar.Event.ApplyEvent(realEvent, newItem, this.regionalSettings);
            
            Assert.AreEqual(item[SPBuiltInFieldId.fRecurrence],
                newItem[SPBuiltInFieldId.fRecurrence]);

            Assert.AreEqual(item[SPBuiltInFieldId.StartDate],
                newItem[SPBuiltInFieldId.StartDate]);
            
            Assert.AreEqual(item[SPBuiltInFieldId.RecurrenceData],
                newItem[SPBuiltInFieldId.RecurrenceData]);
        }

        [TestMethod]
        public void WriteDailyByWeekdayEvent()
        {
            var item = this.calendar.GetItemById(6);
            var evt = WindStyle.SPCalendar.Event.ParseEvent(item, this.regionalSettings);
            var realEvent = evt as WindStyle.SPCalendar.Event<WindStyle.SPCalendar.Pattern.DailyByWeekdayPattern>;

            var newItem = this.calendar.AddItem();
            WindStyle.SPCalendar.Event.ApplyEvent(realEvent, newItem, this.regionalSettings);


            //Assert.AreEqual(item[SPBuiltInFieldId.EndDate],
            //    newItem[SPBuiltInFieldId.EndDate]);

            Assert.AreEqual(item[SPBuiltInFieldId.RecurrenceData],
                newItem[SPBuiltInFieldId.RecurrenceData]);
        }

        [TestMethod]
        public void WriteWeeklyEvent()
        {
            var item = this.calendar.GetItemById(7);
            var evt = WindStyle.SPCalendar.Event.ParseEvent(item, this.regionalSettings);
            var realEvent = evt as WindStyle.SPCalendar.Event<WindStyle.SPCalendar.Pattern.WeeklyPattern>;

            var newItem = this.calendar.AddItem();
            WindStyle.SPCalendar.Event.ApplyEvent(realEvent, newItem, this.regionalSettings);


            //Assert.AreEqual(item[SPBuiltInFieldId.EndDate],
            //    newItem[SPBuiltInFieldId.EndDate]);

            Assert.AreEqual(item[SPBuiltInFieldId.RecurrenceData],
                newItem[SPBuiltInFieldId.RecurrenceData]);
        }

        
        [TestMethod]
        public void WriteMonthlyByFrequencyEvent()
        {
            var item = this.calendar.GetItemById(8);
            var evt = WindStyle.SPCalendar.Event.ParseEvent(item, this.regionalSettings);
            var realEvent = evt as WindStyle.SPCalendar.Event<WindStyle.SPCalendar.Pattern.MonthlyByFrequencyPattern>;

            var newItem = this.calendar.AddItem();
            WindStyle.SPCalendar.Event.ApplyEvent(realEvent, newItem, this.regionalSettings);
            
            Assert.AreEqual(item[SPBuiltInFieldId.RecurrenceData],
                newItem[SPBuiltInFieldId.RecurrenceData]);
        }

        [TestMethod]
        public void WriteMonthlyByDayEvent()
        {
            var item = this.calendar.GetItemById(9);
            var evt = WindStyle.SPCalendar.Event.ParseEvent(item, this.regionalSettings);
            var realEvent = evt as WindStyle.SPCalendar.Event<WindStyle.SPCalendar.Pattern.MonthlyByDayPattern>;

            var newItem = this.calendar.AddItem();
            WindStyle.SPCalendar.Event.ApplyEvent(realEvent, newItem, this.regionalSettings);

            Assert.AreEqual(item[SPBuiltInFieldId.RecurrenceData],
                newItem[SPBuiltInFieldId.RecurrenceData]);
        }

        [TestMethod]
        public void WriteYearlyByFrequencyEvent()
        {
            var item = this.calendar.GetItemById(10);
            var evt = WindStyle.SPCalendar.Event.ParseEvent(item, this.regionalSettings);
            var realEvent = evt as WindStyle.SPCalendar.Event<WindStyle.SPCalendar.Pattern.YearlyByFrequencyPattern>;

            var newItem = this.calendar.AddItem();
            WindStyle.SPCalendar.Event.ApplyEvent(realEvent, newItem, this.regionalSettings);

            Assert.AreEqual(item[SPBuiltInFieldId.RecurrenceData],
                newItem[SPBuiltInFieldId.RecurrenceData]);

        }

        [TestMethod]
        public void WriteYearlyByDayEvent()
        {
            var item = this.calendar.GetItemById(11);
            var evt = WindStyle.SPCalendar.Event.ParseEvent(item, this.regionalSettings);
            var realEvent = evt as WindStyle.SPCalendar.Event<WindStyle.SPCalendar.Pattern.YearlyByDayPattern>;

            var newItem = this.calendar.AddItem();
            WindStyle.SPCalendar.Event.ApplyEvent(realEvent, newItem, this.regionalSettings);

            Assert.AreEqual(item[SPBuiltInFieldId.RecurrenceData],
                newItem[SPBuiltInFieldId.RecurrenceData]);
        }
    }
}
